//
//  CommonPickerModel.swift
//  EyeVisionForDoctors
//
//  Created by QDSG on 2020/3/2.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import HandyJSON

class CommonPickerViewModel: SEBaseViewModel {

    var dataSource = [CommonPickerModel]()

    var provinceId: String = ""
    var cityId: String = ""
    var regionId: String = ""

    func requestForDepts() {
        SEHttpManager.shared.request(SECommonPikcerAPI.listOfAllDepartments, model: [SEDepartmentItem].self, completion: { (models, status) in
            switch status {
            case .success:
                var pickerModels = [CommonPickerModel]()
                for item in models ?? [SEDepartmentItem]() {
                    let model = CommonPickerModel(code: item.departmentId ?? "", name: item.departmentName ?? "")
                    pickerModels.append(model)
                }
                self.dataSource = pickerModels
            case .auth(let message):
                SEHUD.showError(message: message)
            case .failure(let message):
                SEHUD.showError(message: message)
            }
            self.updateData?(status)
        })
    }

    func requestForHospitals() {
        SEHttpManager.shared.request(SECommonPikcerAPI.listOfHospitals(provinceId: provinceId, cityId: cityId, regionId: regionId), model: [SEHospitalModel].self, completion: { models, status in
            switch status {
            case .success:
                var pickerModels = [CommonPickerModel]()
                for item in models ?? [SEHospitalModel]() {
                    let model = CommonPickerModel(code: item.orgId, name: item.name)
                    pickerModels.append(model)
                }
                self.dataSource = pickerModels
            case .auth(let message):
                SEHUD.showError(message: message)
            case .failure(let message):
                SEHUD.showError(message: message)
            }
            self.updateData?(status)
        })
    }

    func requestForJobTitles() {
        SEHttpManager.shared.request(SECommonPikcerAPI.listOfJobTitles, model: [SEJobTitleModel].self) { models, status in
            switch status {
            case .success:
                var pickerModels = [CommonPickerModel]()
                for item in models ?? [SEJobTitleModel]() {
                    let model = CommonPickerModel(code: item.code, name: item.name)
                    pickerModels.append(model)
                }
                self.dataSource = pickerModels
            case .auth(let message):
                SEHUD.showError(message: message)
            case .failure(let message):
                SEHUD.showError(message: message)
            }
            self.updateData?(status)
        }
    }
    
    var mark = ""
    
    func getUsageListOfPrescriptionDrug() {
        SEHttpManager.shared.request(SECommonPikcerAPI.usageOfPrescriptionDrug(mark: mark), model: [CommonPickerModel].self) { (models, httpStatus) in
            switch httpStatus {
            case .success:
                self.dataSource = models ?? []
            case .failure(let message):
                SEHUD.showError(message: message)
            case .auth: break
            }
            self.updateData?(httpStatus)
        }
    }
}

struct CommonPickerModel: HandyJSON {
    var code: String = ""
    var name: String = ""
    var icdId: String = ""
    var helpCode: String = ""
    
    // MARK: - 药品用途
    var key: String?
    var value: String?
}

struct SEDepartmentItem: HandyJSON {
    var id: String?
    var pid: String?

    var departmentId: String?   // 科室id
    var departmentName: String? // 科室名称

    mutating func mapping(mapper: HelpingMapper) {
        mapper <<<
            self.departmentId <-- "key"

        mapper <<<
            self.departmentName <-- "value"
    }
}

struct SEHospitalModel: HandyJSON {
    var orgId: String = ""
    var name: String = ""
}

struct SEJobTitleModel: HandyJSON {
    var code: String = ""
    var name: String = ""
}
